<%--
  Created by IntelliJ IDEA.
  User: Lin
  Date: 2021/4/21
  Time: 10:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>预约</title>
    <link rel="stylesheet" href="${pageContext.servletContext.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.servletContext.contextPath}/webjars/jquery/3.1.1/jquery.min.js"></script>
    <script src="${pageContext.servletContext.contextPath}/static/layui/layui.js"></script>
    <%--    <script src="${pageContext.servletContext.contextPath}/static/layui/css/modules/laydate/default/laydate.css"></script>--%>
</head>
<body>
<form class="layui-form"
      lay-filter="makeAnAppointment"
      action="${pageContext.servletContext.contextPath}/reserve/makeAnAppointment">
    <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->

    <div class="layui-form-item" style="width: 200px">
        <label class="layui-form-label">看病类型</label>
        <div class="layui-input-block">
            <select name="diagnoseType" lay-filter="aihao">
                <option value="0">不住院</option>
                <option value="1">住院</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="width: 250px">
        <label class="layui-form-label">科室</label>
        <div class="layui-input-block">
            <select name="medicalOfficeId" lay-filter="aihao">
                <option value="0">呼吸科</option>
                <option value="1">非人类科</option>
            </select>
        </div>
    </div>
    <label class="layui-form-label">预约时间</label>
    <div class="layui-input-inline">
        <input type="text" class="layui-input" name="timeOfAppointment"
               id="timeOfAppointment" placeholder="yyyy-MM-dd">
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">单选框</label>
        <div class="layui-input-block">
            <input type="radio" name="wardType" value="0" title="普通病房" checked>
            <input type="radio" name="wardType" value="1" title="贵宾病房">
        </div>
    </div>
    <div class="layui-form-item layui-form-text" style="width: 324px">
        <label class="layui-form-label">病况描述</label>
        <div class="layui-input-block">
            <textarea name="illnessDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="submit">预约
            </button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
</form>
<%--<script src="${pageContext.servletContext.contextPath}/static/layui/lay/modules/laydate.js"></script>--%>
<script>
    layui.use('form', function () {
        var form = layui.form;
        console.log(form);
        //各种基于事件的操作，下面会有进一步介绍
    });
    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#timeOfAppointment' //指定元素
            , type: 'datetime'
        });
    });
    $(() => {
        $("#submit").on("click", function () {
            let form1 = layui.form;
            let val = form1.val("makeAnAppointment");
            console.log(val);
            if (val.medicalOfficeId == -1) {
                layer.msg("请选择科室");
                return;
            }
            if (val.illnessDesc == "") {
                layer.msg("请填写病况描述");
                return;
            }
            $.ajax({
                url: "${pageContext.servletContext.contextPath}/reserve/makeAnAppointment",
                data: val,
                type: "POST",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    if (data.code != 0) {
                        layer.msg(data.msg);
                        return;
                    }

                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    // 通常 textStatus 和 errorThrown 之中
                    // 只有一个会包含信息
                    //this; // 调用本次AJAX请求时传递的options参数
                }
            });

        });
        $.ajax({
            url: "${pageContext.servletContext.contextPath}/medicalOffice/getAllIdName",
            type: "POST",
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (data.code != 0) {
                    layui.msg(data.msg);
                    return;
                }
                data = data.data;
                let $medicalOfficeId = $("[name='medicalOfficeId']");
                $medicalOfficeId.text("");
                $medicalOfficeId.append("<option value=\"-1\">--请选择--</option>");
                for (let obj in data) {
                    let option = "<option value=\"" + data[obj]["ID"] +
                        "\">" + data[obj]["OFFICE_NAME"] + "</option>";
                    $medicalOfficeId.append(option);
                }
                let date = new Date();
                let datetime = date.getFullYear() + "-" +
                    (date.getMonth() + 1) + "-" + (date.getDate() + 1) + " " +
                    date.getHours() + ":" + date.getMinutes() + ":" +
                    date.getSeconds();
                $("#timeOfAppointment").val(datetime);
                try {
                    layui.form.render();
                } catch (e) {
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中
                // 只有一个会包含信息
                //this; // 调用本次AJAX请求时传递的options参数
            }
        });
    });
    // laydate.render({
    //     elem: '#test1'
    // });
</script>
</body>
</html>
